package com.liu.ai.chat;

import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.ai.retry.RetryUtils;
import org.springframework.web.client.RestClient;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;

import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Array;
import java.util.ArrayList;

public class Test {

    public static void main(String[] args) throws Exception {
        URL url = new URL( "https://dashscope.aliyuncs.com");

        System.out.println(url.getProtocol() +":\\\\"+url.getHost());
        System.out.println(url.getPath());


        OpenAiApi openAiApi = new OpenAiApi(
                "https://dashscope.aliyuncs.com",
                "sk-d1c41de66c074fc686488ab227d32fbe",
                "/compatible-mode/v1/chat/completions",
                "/vi/embeddings",
                RestClient.builder(),
                WebClient.builder(),
                RetryUtils.DEFAULT_RESPONSE_ERROR_HANDLER);


        ArrayList<OpenAiApi.ChatCompletionMessage> msg = new ArrayList<>();
        OpenAiApi.ChatCompletionMessage chat = new OpenAiApi.ChatCompletionMessage("介绍一下你自己", OpenAiApi.ChatCompletionMessage.Role.USER);


        msg.add(chat);
        OpenAiApi.ChatCompletionRequest chatCompletionRequest = new OpenAiApi.ChatCompletionRequest(msg,"qwen-plus",0.7,true);
        Flux<OpenAiApi.ChatCompletionChunk> chatCompletionChunkFlux = openAiApi.chatCompletionStream(chatCompletionRequest);
        chatCompletionChunkFlux.subscribe(System.out::println);


        Thread.sleep(60000);
    }
}
